<?PHP
// vim: set expandtab tabstop=4 shiftwidth=4:
// +----------------------------------------------------------------------+
// | SAPID: XML Sapiens Engine Demonstrator                               |
// +----------------------------------------------------------------------+
// | Author:  Max Baryshnikov aka Mephius <mb@rg.by>	                  |
// | Copyright (c) 2004 Max Baryshnikov                                   |
// | http://sapid.sourceforge.net	                                      |
// +----------------------------------------------------------------------+
// | This source file is free software; you can redistribute it and/or    |
// | modify it under the terms of the GNU Lesser General Public           |
// | License as published by the Free Software Foundation; either         |
// | version 2.1 of the License, or (at your option) any later version.   |
// |                                                                      |
// | This source file is distributed in the hope that it will be useful,  |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
// | Lesser General Public License for more details.                      |
// +----------------------------------------------------------------------+
// Release: 08.09.04 (dd/mm/yy)
// $Id: debug.inc.php,v 1.5 2006/02/20 08:27:31 sheiko Exp $

/**
* @return void
* @param array $code
* @desc Debug function
*/
//define ("FATAL", E_USER_ERROR); 
define ("ERROR", E_USER_WARNING); 
define ("WARNING", E_USER_NOTICE); 

error_reporting(0); 

function this_message_die($title, $message) {
	global $SAPIDTHEME;
	if( !$SAPIDTHEME ) $SAPIDTHEME="default";
	$content = @file_get_contents(ROOT_PATH. "usr/themes/".$SAPIDTHEME."/templates/system_message.tpl");
	if(!$content) return false;
	$content = str_replace(array("&http_path.value;",'<sapi:apply name="window_title.value" />','<sapi:apply name="window_content.value" />'),
	array($GLOBALS["http_path"], $title, $message), $content);
	print $content;
	exit;
}

function this_message_warrning($title, $message) {
	global $error_stack;
	$error_stack .= "<b style=\"color: red;\">".$title."</b><br /><span style=\"font-size: 10px;\">".$message."</span><br />";
}

function userErrorHandler($errno, $errmsg, $filename, $linenum, $vars) { 
	global $sapi_obj;
	
	if($errno==2048) return false;
	
	   $errortype = array ( 
               1   =>  "Error", 
               2   =>  "Warning", 
               4   =>  "Parsing Error", 
               8   =>  "Notice", 
               16  =>  "Core Error", 
               32  =>  "Core Warning", 
               64  =>  "Compile Error", 
               128 =>  "Compile Warning", 
               256 =>  "User Error", 
               512 =>  "User Warning", 
               1024=>  "User Notice" 
               ); 

        
  if($errno!=8 AND $errno!=1024) {
		$message = "  ".$errortype[$errno]."({$errno}) in line $linenum of file $filename";
		$message .= ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
		toLog($message, "a");
  }
  
               
	switch ($errno) {
		case 1:
		case 4:
		case 16:
		case 32:
		case 64:
		case 256:
		$title = "<b>".$errortype[$errno]."</b> [$errno] $errmsg";
		$message = "  ".$errortype[$errno]." in line $linenum of file $filename";
		$message .= ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
		$message .= "Aborting...<br />\n";
		this_message_die($message, $title);
		break;
		case 1024:
			this_message_warrning("NOTICE", "{$errmsg}<br />\n");
		break;
		case 512:
			this_message_warrning("WARNING", "{$errmsg}<br />\n");
		break;		
		case 2:
			// this_message_warrning("WARNING", "$errmsg - $filename:$linenum");
		break;
		case E_NOTICE:
		break;
		default:
		if($errortype[$errno])
		this_message_warrning($errortype[$errno], "[$errno] $errmsg - $filename:$linenum");
		else 
		this_message_warrning("ERROR", "Unkown error type: [$errno] $errmsg - $filename:$linenum<br />\n");
		break;
	}
}

$old_error_handler = set_error_handler("userErrorHandler"); 

if (!function_exists('d')) {
	/**
	 * Show array's content and structure
	 *
	 * @param mixed $var
	 * @return boolean
	 */
	function d($var="<div style=\"color: red;\">EMPTY</a>"){ 
	?><pre style="font-size: 10px;  color: Maroon; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;"><?print_r($var)?></pre><? 
	}
}

if (!function_exists('getMicrotime')) {
/**
 * Returns microtime for execution time checking.
 *
 * @return integer
 */
   function getMicrotime() {
      list($usec, $sec) = explode(" ", microtime());
      return ((float)$usec + (float)$sec);
   }
}
$env["time_start"]=getmicrotime();

?>